package com.op.parcel.manage.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.op.parcel.boot.support.Condition;
import com.op.parcel.boot.support.Query;
import com.op.parcel.common.api.R;
import com.op.parcel.manage.modules.sys.entity.Role;
import com.op.parcel.manage.modules.sys.service.IRoleService;
import com.op.parcel.manage.modules.sys.vo.RoleVO;
import com.op.parcel.manage.modules.sys.wrapper.RoleWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Map;

/**
 * 控制器
 *
 * @author xuan
 * @since 2019-01-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("role")
@Api(value = "", tags = "接口")
public class RoleController {

	private IRoleService roleService;

	/**
	 * 详情
	 */
	@GetMapping("detail")
	@ApiOperation(value = "详情", notes = "传入role", position = 1)
	public R detail(Role role) {
		Role detail = roleService.getOne(Condition.getQueryWrapper(role));
		RoleWrapper roleWrapper = new RoleWrapper();
		return R.success(roleWrapper.entityVO(detail));
	}

	/**
	 * 列表
	 */
	@GetMapping("roles")
	@ApiOperation(value = "列表", notes = "列表", position = 1)
	public R roles(@ApiIgnore @RequestParam Map<String, Object> role) {
		return R.success(roleService.list(Condition.getQueryWrapper(role, Role.class)));
	}

	/**
	 * 分页
	 */
	@GetMapping("list")
	@ApiOperation(value = "分页", notes = "传入role", position = 2)
	public R list(Role role, Query query) {
		IPage<Role> pages = roleService.page(Condition.getPage(query), Condition.getQueryWrapper(role));
		RoleWrapper roleWrapper = new RoleWrapper();
		return R.success(roleWrapper.pageVO(pages));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("saveOrUpdate")
	@ApiOperation(value = "新增或修改", notes = "传入role", position = 3)
	public R saveOrUpdate(@Valid @RequestBody Role role) {
		return R.status(roleService.saveOrUpdate(role));
	}


	/**
	 * 删除
	 */
	@PostMapping("remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids", position = 4)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(roleService.removeByIds(Collections.singleton(ids.split(","))));
	}


}
